<template>
  <div>
    <a-modal
      :visible="visible"
      :title="title"
      @cancel="cancelChange"
      @ok="onSubmit"
      :confirmLoading="confirmLoading"
      :width="600"
    >
      <a-form layout="vertical">
        <a-row :gutter="[20, 0]">
          <a-col :span="12">
            <a-form-item label="分类名称" v-bind="validateInfos.cat_name">
              <a-input
                v-model:value="modelRef.cat_name"
                placeholder="请输入分类名称"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="上级分类" v-bind="validateInfos.parent_id">
              <a-select
                v-model:value="modelRef.parent_id"
                placeholder="请选择上级分类"
                :options="parentOptions"
              ></a-select>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="分类排序" v-bind="validateInfos.cat_sort">
              <a-input-number
                style="width: 100%"
                v-model:value="modelRef.cat_sort"
                :min="0"
                :precision="0"
                placeholder="请输入排序"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="分类状态" v-bind="validateInfos.cat_status">
              <a-switch
                v-model:checked="modelRef.cat_status"
                checked-children="开"
                un-checked-children="关"
                :checkedValue="1"
                :unCheckedValue="0"
              />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-modal>
  </div>
</template>

<script>
import { defineComponent, ref, toRefs, computed, watch, reactive } from "vue";
import request from "@/common/utils/request";
import { Form, message } from "ant-design-vue";
const useForm = Form.useForm;

export default defineComponent({
  name: "AddBrand",
  components: {},
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    id: {
      type: [String, Number],
      default: 0,
    },
  },
  setup(props, context) {
    const state = reactive({
      count: 0,
      parentOptions: [],
      confirmLoading: false,
    });

    const modelRef = reactive({
      cat_name: "",
      parent_id: 0,
      cat_sort: "",
      cat_status: 1,
    });
    const rulesRef = reactive({
      cat_name: [
        {
          required: true,
          message: "请输入分类名称",
        },
      ],
      parent_id: [
        {
          required: true,
          message: "请选择上级分类",
        },
      ],
    });

    const { resetFields, validate, validateInfos } = useForm(
      modelRef,
      rulesRef
    );

    const cancelChange = () => {
      resetFields();
      context.emit("close");
    };

    const onSubmit = () => {
      validate().then(() => {
        state.confirmLoading = true;
        if (props.id) {
          request
            .put(
              "/org",
              `/org/brand/custom/brand/category/${props.id}`,
              modelRef
            )
            .then((res) => {
              state.confirmLoading = false;
              message.success(res.msg || "修改成功");
              resetFields();
              context.emit("success");
            })
            .catch((err) => {
              state.confirmLoading = false;
            });
          return;
        }
        request
          .post("/org", "/org/brand/custom/brand/category", modelRef)
          .then((res) => {
            state.confirmLoading = false;
            message.success(res.msg || "保存成功");
            resetFields();
            context.emit("success");
          })
          .catch((err) => {
            state.confirmLoading = false;
          });
      });
    };

    const title = computed(() => {
      return props.id ? "编辑品牌" : "添加品牌";
    });

    const getBrandList = () => {
      request
        .post("/org", "/org/brand/custom/brand/category/query", { filters: [] })
        .then((res) => {
          state.parentOptions = res.list.map((item) => {
            return {
              label: item.cat_name,
              value: item.id,
            };
          });
          state.parentOptions.unshift({
            label: "无",
            value: 0,
          });
        });
    };

    const getCategoryDetail = () => {
      request
        .get("/org", `/org/brand/custom/brand/category/${props.id}`)
        .then((res) => {
          sendModelRef(res.detail);
        });
    };

    const sendModelRef = (data) => {
      for (let key in data) {
        if (modelRef.hasOwnProperty(key)) {
          modelRef[key] = data[key];
        }
      }
    };

    watch(
      () => props.visible,
      (val) => {
        if (val) {
          getBrandList();
          if (props.id) {
            getCategoryDetail();
          }
        }
      }
    );

    return {
      ...toRefs(state),
      title,
      onSubmit,
      cancelChange,
      validateInfos,
      modelRef,
    };
  },
});
</script>

<style lang="less" scoped></style>
